# -*- coding:utf-8 -*-
from flask import Flask, request, jsonify, abort
from flask_cors import CORS
from com.zzh.dao.processDao import ProcessAPI

from . import app

CORS(app, supports_credentials=True)

'''项目订单流程表 zzh_process API'''


@app.route('/query-process/<int:index>', methods=['POST'])  # 分页查询process表。疑问：按照什么规则查询？？？
def queryProcess(index):
    queryResultOfDatabaseProcess, processListJson = ProcessAPI().queryProcess(index)  # 返回值的获取顺序要匹配
    if queryResultOfDatabaseProcess:
        return jsonify({"message": {"code": 1, "info": "已查询到订单流程"}, "processListJson": processListJson,
                        "totalCount": queryResultOfDatabaseProcess})
    else:
        return jsonify({"message": {"code": 0, "info": "未查询到订单流程"}})


@app.route('/add-process', methods=['POST'])  # process表添加记录
def addProcess():
    if not request.json:
        print request.json
        abort(400)
    addProcess = {
        'processSum': request.json.get('processSum', 0),  # processSum  流程的总步数
        'processCode': request.json.get('processCode', 0),  # processCode 流程编号
        'processStatus': request.json.get('processStatus', ""),  # processStatus  流程状态
        'processName': request.json.get('processName', '')
    }
    addProcessResult = ProcessAPI().addProcess(addProcess)
    if addProcessResult:
        return jsonify({"addProcessInfo": addProcess,
                        "massage": {"code": 1, "info": "addProcesss OK"}})  # 添加函数调用成功，返回添加的信息（不包括process_id）
    else:
        return jsonify({"message": {"code": 0, "info": "addProcesss Failed"}})  # 添加函数调用失败


@app.route('/update-process/<int:processId>', methods=['POST'])  # 根据processId更新process表指定记录
def updateProcess(processId):
    newUpdateProcess = {
        'processSum': request.json.get('processSum', 0),  # processSum  流程的总步数
        'processCode': request.json.get('processCode', 0),  # processCode 流程编号
        'processStatus': request.json.get('processStatus', ""),  # processStatus  流程状态
        'processName': request.json.get('processName', '')
    }
    updateProcessResult = ProcessAPI().updateProcessByProcessId(newUpdateProcess, processId)
    if updateProcessResult:
        return jsonify({"updateProcessInfo": newUpdateProcess, "massage": {"code": 1, "info": "updateProcesss OK"}})
    else:
        return jsonify({"message": {"code": 0, "info": "updateProcesss Failed"}})


@app.route('/delete-process/<int:processId>', methods=['DELETE'])  # 根据processId删除process表指定记录
def deleteProcess(processId):
    deleteProcessResult = ProcessAPI().deleteProcessById(processId)
    if deleteProcessResult:
        return jsonify({"message": {"code": 1, "info": "删除操作成功！"}})
    else:
        return jsonify({"message": {"code": 0, "info": "删除操作失败！"}})
